package com.kun.service_edu.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.service_edu.entity.EduTeacher;
import com.kun.service_edu.entity.vo.TeacherQuery;
import com.kun.service_edu.service.EduTeacherService;
import com.kun.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-09-21
 */
@Api(value = "讲师管理")
@RestController
@RequestMapping("/service_edu/edu-teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "查询所有")
    @GetMapping("findAll")
    public R findAll() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }

    ;

    @ApiOperation(value = "删除一个")
    @DeleteMapping("del/{id}")
    public R del(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id) {

        boolean b = eduTeacherService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @GetMapping("pageFind/{current}/{limit}")
    public R pageFind(@PathVariable Long current,@PathVariable Long limit){
        Page<EduTeacher> page = new Page<>(current,limit);
         eduTeacherService.page(page, null);
         long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return R.ok().data(map);
    }

    @PostMapping("/PageTeacherQuery/{current}/{limit}")
    public R PageTeacherQuery(@PathVariable Long current, @PathVariable Long limit,@RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<>(current,limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        Date gmtCreate = teacherQuery.getGmtCreate();
        Date gmtModified = teacherQuery.getGmtModified();

        if (!ObjectUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (!ObjectUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }
        if (!ObjectUtils.isEmpty(gmtCreate)){
            queryWrapper.ge("gmtCreate",gmtCreate);
        }
        if (!ObjectUtils.isEmpty(gmtModified)){
            queryWrapper.le("gmtModified",gmtModified);
        }
        queryWrapper.orderByDesc("id");
        eduTeacherService.page(page, queryWrapper);

        Long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return R.ok().data(map);
    }

    @PostMapping("addTeacher")

    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }

    }
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable Long id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = eduTeacherService.updateById(eduTeacher);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

